home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4464 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  138 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Halp! I don't know what I'm doing wrong!
  5. Date: 4 Feb 1996 21:29:28 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4f38fo$6ri@news.iag.net>
  8. References: <4eu8sl$f27@aphex.direct.ca>
  9. NNTP-Posting-Host: pm3-orl21.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4eu8sl$f27@aphex.direct.ca>, etoivane@direct.ca says...
  13. >
  14. >I've been at this for a couple *days* and can't get it to work.
  15. >It's supposed to graph a function entered at the command line,
  16. >eg. "c:\>graph 2 1" should graph out 2x+1 to stdout using the '*'
  17. >character.  Can anybody help me please?
  18. >I'll name all my kids after you!(when I have 'em that is!) 
  19. >
  20. >/*
  21. >        Ed Toivanen
  22. >        Comp3425
  23. >        Assign1
  24. >*/
  25. >
  26. >#include <stdio.h>
  27. >#include <stdlib.h>
  28. >
  29. >#define DOMAN 80
  30.  
  31. Depending on your output device, this value might cause double-spacing.
  32.  
  33. >#define DCORR 40/* shift right, to keep in domain of array*/
  34. >#define RANGE 40
  35. >#define RCORR 20/* shift up, to keep in range of array*/
  36. >
  37. >long power(long, int);
  38. >
  39. >int main(int argc, char **argv){
  40. >    long minDomain = -DOMAN/2;
  41. >    long maxDomain = DOMAN/2-1;
  42. >    long minRange = -RANGE/2;
  43. >    long maxRange = RANGE/2-1;
  44. >    static char lin[RANGE][DOMAN]; /*
  45. >    long x, y=0;
  46. >    long degree, argctemp=argc;
  47. >
  48. >    if(argc < 2){
  49. >        printf("Usage %s <coefficients>\n", *argv);
  50. >        return(0);
  51. >    }
  52. >         
  53. >    for(x=minDomain; x<maxDomain; x++){
  54. >        degree = argc - 1;
  55.  
  56. You need to reset y to 0 on each iteration of the for loop.
  57.  
  58. >        while(degree--){
  59. >            y = ((atol(*++argv)) * (power(x, degree))) + y;
  60.  
  61. Because you are actually incrementing argv, after the first iteration of 
  62. the for loop, you will no longer be looking at valid pointers. Try something
  63. like:
  64.  
  65.   atol(argv[argc - degree - 1])
  66.  
  67. You are getting a skewwed results. (eg, the point 0,0 with the args 0 1) You
  68. might want to drop out of the while loop before the final argument and simply
  69. add it to y instead of running through power.
  70.  
  71. My compiler warns about degree being long, when power accepts an int.  It is
  72. not relevant in the range of values present (degree will not exceed INT_MAX),
  73. but you might want to match the types anyway.
  74.  
  75. >            if(y > maxRange || y < minRange)
  76. >                y = 0;
  77.             
  78. ?? Won't this cause a point not in the line to be plotted?
  79.             
  80. >        }
  81.  
  82. You need to add a condition to protect your array bounds here. Something like
  83.  
  84.    if (y >= minRange && y <= maxRange) 
  85.  
  86. >        lin[ y + RCORR][x + DCORR] = '*';
  87. >    }
  88. >     
  89. >    for(y=maxRange; y>minRange; y--){/*from top down*/
  90. >       for(x = minDomain; x < maxDomain; x++){
  91. >          printf("%c", lin[y + RCORR][x + DCORR]);
  92.  
  93. Because lin is defined as static, all elements are initialized to '\0'. So
  94. this is sending '\0's to stdout.  I am not sure what the result will be on
  95. your system, but I suspect that it will not be what you want.
  96.  
  97. >       }
  98. >       printf("\n");
  99. >    }
  100.  
  101. If you will define lin with one extra char in each line to allow for a '\0',
  102. then prior to your plotting loop, use memset in a for loop to initialize all 
  103. chars in each line to ' ' (except for the terminating '\0'), you will be able
  104. to replace this nested loop with a single loop that steps through the RANGE 
  105. and prints each line.  
  106.  
  107. >    return(0);
  108. >}
  109. >
  110. >long power(long num, int degree){/* for positive, 
  111. >whole powers*/
  112. >    long product = 1L;
  113. >    
  114.  
  115. You should test for illegal degree values and handle the possibility
  116.  
  117. >    if(degree==0 && num==0)
  118.  
  119. The degree condition is not necessary. 0 to any power is 0.
  120.  
  121. >        return(0L);
  122. >    if(degree==0)
  123. >        return(1L);
  124.  
  125. The while condition and initialization of product make this test unnecessary
  126.  
  127. >    while(degree--){
  128. >        product *= num;
  129. >    }
  130. >    return(product);
  131. >}                      
  132. >
  133.  
  134. -- 
  135. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  136. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  137.  
  138.